package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Balance;
import com.example.demo.entity.BalanceArea;
import com.example.demo.mapper.BalanceAreaMapper;
import com.example.demo.service.BalanceAreaService;
import com.example.demo.service.BalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Atanycosts
* @description 针对表【balance_area】的数据库操作Service实现
* @createDate 2023-05-27 17:02:05
*/
@Service
public class BalanceAreaServiceImpl extends ServiceImpl<BalanceAreaMapper, BalanceArea>
    implements BalanceAreaService {

    @Autowired
    private BalanceAreaMapper balanceAreaMapper;

    @Autowired
    private BalanceService balanceService;


    @Override
    public List<Balance> getAllBalanceArea(String province) {

        //在方案区域表中 只查询出BalanceID
        List<Integer> balanceIds = new LambdaQueryChainWrapper<BalanceArea>(balanceAreaMapper)
                .eq(BalanceArea::getProvince, province)
                .list()
                .stream()
                .map(BalanceArea::getBalanceId)
                .collect(Collectors.toList());

        if (balanceIds == null) {
            throw new RuntimeException("此地区方案为空");
        }
        List<Balance> balances = new ArrayList<>();
        balanceIds.stream().map((item)->{
            balances.add(balanceService.getById(item));
            return balances;
        }).collect(Collectors.toList());


        return balances;
    }
}




